package com.mzy.xmyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mzy.xmyx.common.result.Result;
import com.mzy.xmyx.acl.service.RoleService;
import com.mzy.xmyx.model.acl.Role;
import com.mzy.xmyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation("获取角色分页列表(带搜索)")
    @GetMapping("/{current}/{limit}")
    public Result pageList(@PathVariable Integer current,
                           @PathVariable Integer limit,
                           RoleQueryVo roleQueryVo) {
        IPage<Role> pageModel = roleService.selectPage(current, limit, roleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("通过id查询角色")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    @ApiOperation("保存一个新角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        boolean isSuccess = roleService.save(role);
        return Result.ok(isSuccess);
    }

    @ApiOperation("更新一个角色")
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        boolean isSuccess = roleService.updateById(role);
        return Result.ok(isSuccess);
    }


    @ApiOperation("删除某个角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = roleService.removeById(id);
        return Result.ok(isSuccess);
    }


    @ApiOperation("批量删除多个角色")
    @PutMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean isSuccess = roleService.removeByIds(ids);
        return Result.ok(isSuccess);
    }
}
